#ifndef __DSTH_WAKEUP_H__
#define __DSTH_WAKEUP_H__

#include <memory>
#include "Timestamp.h"
#include "InetSocketAddress.h"

namespace dsth {
class EventLoop;
class Connector;
class Channel;
class Acceptor;
class Wakeup {
public:
    explicit Wakeup(EventLoop* loop);
    ~Wakeup();
    void AttachToLoop();
    void Waken();
private:
    // Can't invoke copy constructor or assignment operator
    Wakeup(const Wakeup&);
    void operator=(const Wakeup&);

    void HandleSelfConnection(int fd);
    void CreateWakeupListener();
    void SelfConnected(int sockfd, const InetSocketAddress& peer_addr);
    void ConnectToSelf();
    void OnReadable(Timestamp time);

    EventLoop* loop_;
    unsigned short self_port_;
    bool is_self_conn_;
    std::auto_ptr<Connector> conn_;
    std::auto_ptr<Acceptor> wakeup_listener_;
    std::auto_ptr<Channel> listen_channel_;
    std::auto_ptr<Channel> wake_channel_;
    int wakefd_;
    int readfd_;
};

} // namespace
#endif // __DSTH_WAKEUP_H__
